package com.yidu.ladder.delivery.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.delivery.dao.CooperationDao;
import com.yidu.ladder.delivery.domain.Cooperation;
import com.yidu.ladder.delivery.domain.CooperationVo;
import com.yidu.ladder.delivery.service.CooperationService;

/**
 * 合作网络业务逻辑接口实现类
 * 
 * @author yy
 * @since 2019-12-20
 *
 */
@Service
public class CooperationServiceImpl implements CooperationService {

	/**
	 * 关联合作网络数据访问层
	 */
	@Autowired
	private CooperationDao cooperationDao;

	public CooperationDao getCooperationDao() {
		return cooperationDao;
	}

	public void setCooperationDao(CooperationDao cooperationDao) {
		this.cooperationDao = cooperationDao;
	}

	/**
	 * 查询合作网络
	 */
	@Override
	public Map<String, Object> queryCooperationByCriteriaWithPage(CooperationVo cooperationVo) {
		// 计算分页
		int page = (cooperationVo.getPage() - 1) * cooperationVo.getLimit();
		cooperationVo.setPage(page);
		// 查询满足检索条件的班车
		List<CooperationVo> cooperationList = cooperationDao.queryCooperationByCriteriaWithPage(cooperationVo);
		// 统计用户总数
		Integer total = cooperationDao.countCooperationByCriteria(cooperationVo);
		// 创建返回结果映射: {"total":"用户总数",rows:"指定页面的用户集合"}
		HashMap<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("code", 0);
		returnMap.put("msg", " ");
		returnMap.put("count", total);
		returnMap.put("data", cooperationList);
		return returnMap;
	}

	/**
	 * 添加合作网络
	 */
	@Override
	public int addCooperation(Cooperation cooperation) {
		return cooperationDao.addCooperation(cooperation);
	}

	/**
	 * 修改合作网络
	 */
	@Override
	public boolean updateCooperation(Cooperation cooperation) {
		return cooperationDao.updateCooperation(cooperation) == 1 ? true : false;
	}

	/**
	 * 冻结或还原合作网络
	 */
	@Override
	public boolean frozenOrRecoveCooperation(String cooperationId, String useable) {
		String[] cooperationIdArr = cooperationId.split(",");
		int totle = 0;
		for (String cId : cooperationIdArr) {
			// 创建一个对象
			Cooperation cooperation = new Cooperation();
			// 赋值
			cooperation.setCooperationId(cId);
			;
			cooperation.setUseable(useable);
			// 调用修改班车方法
			int result = cooperationDao.updateCooperation(cooperation);
			if (result == 1) {
				totle++;
			}
		}
		return totle == cooperationIdArr.length ? true : false;
	}

}
